const Type = require("../models/Type");
const { Op } = require("sequelize");
const validate = require("validate.js");
const { pick } = require("../util/propertyHelper");
exports.addType = async function (typeObj) {
  const ins = await Type.create(typeObj);
  return ins.toJSON()
};

exports.deleteType = async function (id) {
    await Type.destroy({
        where:{
            id
        }
    })
};

exports.updateType = async function (id,typeObj) {
    await Type.update(typeObj,{
        where:{
            id
        }
    })
};

//分页查询
exports.findTypes = async function (search = "") {
    const condition = {};
    if (search) condition.name = { [Op.like]: `%${search}%` };
    const result = await Type.findAndCountAll({
      where: condition,
    });
    return {
      total: result.count,
      list: JSON.parse(JSON.stringify(result.rows)),
    };
  };
  
  exports.findTypeById = async function (id) {
    const result = await Type.findByPk(id);
    return result.toJSON();
  };
